package com.xy.modulemain.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.xy.modulemain.R
import com.xy.modulemain.bean.Item
import com.xy.modulemain.view.ICustomerView
import com.xy.modulemain.view.ImageItemView
import com.xy.modulemain.view.TitleItemView
import com.xy.modulemain.viewholder.BaseViewHolder
import kotlinx.android.synthetic.main.item_view.view.*

/**
 * @ClassName RecyclerViewAdapter
 * @Author xy
 * @Date 2021/8/20 10:32
 * @Description RecyclerView  适配器
 */

const val IMAGE = 1
const val TEXT = 0

 class RecyclerViewAdapter(private var mContext: Context) :
    RecyclerView.Adapter<BaseViewHolder>() {
    private var listItem = mutableListOf<Item>()
    fun setData(list: MutableList<Item>) {
        listItem = list
        notifyDataSetChanged()
    }

    //    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        var baseViewHolder: BaseViewHolder
        when (viewType) {
            IMAGE -> {
                val imageItemView = ImageItemView(parent.context) as ICustomerView<Item>
                baseViewHolder = BaseViewHolder(imageItemView)
            }
            TEXT -> {
                val titleItemView = TitleItemView(parent.context) as ICustomerView<Item>
                baseViewHolder = BaseViewHolder(titleItemView)

            }
            else -> {
                val imageItemView = ImageItemView(parent.context) as ICustomerView<Item>
                baseViewHolder = BaseViewHolder(imageItemView)
            }

        }

        return baseViewHolder

//        var view = LayoutInflater.from(mContext).inflate(R.layout.item_view,parent,false)
//       return TitleViewHolder(view)
    }


    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        holder.bind(listItem[position])


//        if (holder is TitleViewHolder){
//            holder.text.text = listItem[position].context
//            holder.img.tag = listItem[position].tag
//
//            if (listItem[position].imgUrl.isNotEmpty()){
//                if (holder.img.tag==listItem[position].tag){
//                    //设置图片
//                    GlideUtils.showNormalCenterCropImage(listItem[position].imgUrl,holder.img,R.mipmap.ic_launcher)
//                }
//            }else{
//                //为空的时候设置默认值图片
//                GlideUtils.showNormalCenterCropImage(listItem[position].imgUrl,holder.img,R.mipmap.ic_launcher)
//            }
//        }

    }

    override fun getItemCount(): Int {
        return listItem.size
    }


    override fun getItemViewType(position: Int): Int {
        return if (listItem[position].isHasPic == 1) {
            IMAGE
        } else {
            TEXT
        }
    }

    class TitleViewHolder : RecyclerView.ViewHolder {
        var text: TextView
        var img: ImageView

        constructor(itemView: View) : super(itemView) {
            text = itemView.findViewById(R.id.text)
            img = itemView.findViewById(R.id.img)
            itemView.text.setOnClickListener {
                ToastUtils.showShort("${itemView.text}被点击了")
            }
        }


    }
}